<?php
 /******************************************************************************
 *   GunCMS is an avid supporter of open source software. This is the appropriate 
 *   option if you are creating an open source application with a license 
 *   compatible with the GNU GPL license v3.
 *   For details http://www.gnu.org/licenses/gpl.html
 *   DO NOT USE FOR COMMERCIAL WITHOUT PERMISSION
 *
 * GunCMS Version 1.0.3
 * Copyright(c) 2009-2010, T & L GdbR
 * info@guncms.de
 * 
 * http://www.guncms.de
 ********************************************************************************/
class BWebshopInComingOrder  {

    var $db;
 	var $smarty;
 	
    function __construct($db, $smarty) {
    	$this->db = $db;
		$this->smarty = $smarty;
    }
	
	public function EditInComingOrder() {
		if(isset( $_GET['id'])) {
			$msgId = 2;
			$msg = "";				 
			if($msg == "") {
				
				$res = BWebshopInComingOrderBean::update_incoming_order($this->db, $_POST);
				$message = "Order has been updated";
				setcookie("message", $message,time()+5);
				/*check edit category top or left*/
				header("Location: ./?module=BgcmsWebshop&sub1=incomingorder");	

			} else {
				//we set msgId to 99 to indicate message will load in session variable
				$message = "Order has not been updated";
				setcookie("message", $message,time()+5);
				header("Location: ./?module=BgcmsWebshop&sub1=incomingorder&act=editincomingorder&message=" . $msgId . "&id=" . $_GET['id']);
			}
			
		}
	}
	
	public function SendPDF() {
		require('../includes/libraries/fpdf/html2fpdf.php');
		$pdf=new HTML2FPDF();
		$pdf->AddPage();
		$pdf->SetFont('Arial');
		$single_order = BWebshopInComingOrderBean::get_incoming_order_by_id($this->db, $_POST['id']);
		$orderinfo_product = BWebshopInComingOrderBean::get_incoming_order_product($this->db, $single_order->so_id);
		$single_order->product = $orderinfo_product;
		
		$this->smarty->assign("ORDER",$single_order);				
		$content = $this->smarty->fetch("BgcmsWebshop/InComingOrder/form_pdf.tpl");
		$name = $_POST['id'];	
		$path = "../upload/customer/billing/"; 
		$cusemail = $single_order->customer_email;  
		$pdf->WriteHTML($content);
		$pdf->Output($path.$name.".pdf");
		$name = $_POST['id'];
		$atta['0']['file'] = $path.$name.".pdf";
		$atta['0']['file_name'] = $name.".pdf";
		$stradmin = $this->smarty->fetch("BgcmsWebshop/InComingOrder/email_order.tpl");
		BgcmsEmail::send_mail($cusemail, $stradmin, $subject, $from, $fromname,$atta);
		$arr['ID'] = 0;
		$arr['message'] = sprintf(_("Email has been sent to ".$cusemail." ."));
		
		echo array2json($arr);
	}
	
	public function DeleteInComingOrder() {
		$arr = array('ID' => 0, 'message' => "");
		$id = @$_POST['id'];
		$res = BWebshopInComingOrderBean::delete_incoming_order_by_id($this->db, $id);
		if($res == true) {
			$arr['ID'] = 0;
			$arr['message'] = sprintf(_("Incoming Order <u>%s</u> has been deleted."), $id);
		} else {
			$arr['ID'] = -1;
			$arr['message'] = sprintf(_("Could not delete Incoming Order <u>%s</u>."), $id);
		}
		echo array2json($arr);
	}
	
	public function ChangePaidStatus() {
		$arr = array('ID' => 0, 'message' => "");
		$id = @$_POST['id'];
		$res = BWebshopInComingOrderBean::update_status_paid($this->db, $id,$_POST['value']);
		if($res == true) {
			$arr['ID'] = 0;
			$arr['message'] = sprintf(_("Incoming Order <u>%s</u> has been updated."), $id);
		} else {
			$arr['ID'] = -1;
			$arr['message'] = sprintf(_("Could not update Incoming Order <u>%s</u>."), $id);
		}
		echo array2json($arr);
	}
}
?>